package org.colcon.web.client.ui.deprecated;

import org.colcon.web.client.Collective_Congress;
import org.colcon.web.client.ui.ClickablePanel;
import org.colcon.web.client.ui.panel.LoginPanel;
import org.colcon.web.model.Item;
import org.colcon.web.model.framework.IGroup;
import org.colcon.web.model.framework.IItem;
import org.colcon.web.model.framework.ITitle;
import org.colcon.web.model.framework.IUser;
import org.colcon.web.model.framework.IVote;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;

public class NamePanel extends ClickablePanel 
{
	private String item;
	
	public NamePanel(Collective_Congress client, String item) 
	{
		this(client, item, item);
	}
	
	public NamePanel(Collective_Congress client, String item, String text) 
	{
		super(client, "name", text);
		
		this.item = item;
	}
	
	public String getItem()
	{
		return item;
	}

	public void click() 
	{
		getBodyPanel().clear();
		
		if(!getClient().loggedIn())
		{
			getBodyPanel().add(new LoginPanel(getClient()));
			return;
		}
		
		getBodyPanel().add(new Label("Loading..."));
		
		getService().get(item, new AsyncCallback<Item>()
		{
			public void onFailure(Throwable caught)
			{
				getBodyPanel().clear();
				getBodyPanel().add(new Label("failed to get item " + item));
			}
			
			public void onSuccess(Item item)
			{
				getBodyPanel().clear();
				
				if(item != null)
				{
					getBodyPanel().add(getInfoPanel(item));
				}
				else
				{
					getBodyPanel().add(new Label("failed to get item " + item));
				}
			}
		});
	}
	
	public InfoPanel getInfoPanel(IItem item)
	{
		if(item instanceof IUser)
			return new UserInfoPanel(getClient(), (IUser) item);
		
		if(item instanceof IGroup)
			return new GroupInfoPanel(getClient(), (IGroup) item);
		
		if(item instanceof ITitle)
			return new TitleInfoPanel(getClient(), (ITitle) item);
		
		if(item instanceof IVote)
			return new ElectionPanel(getClient(), (IVote) item);
		
		//TODO: is PowerNamePanel necessary?
		//if(item instanceof IPower)
		//	use PowerNamePanel
		
		return new InfoPanel(getClient(), item);
	}
}
